/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserManager;
import DBManager.DBConnection;
import java.security.MessageDigest;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 *
 * @author Farley
 */
public class UserManager {
    
    private int checker;
    
     public boolean s_logIn(String username,String password)
    {
        Student newStudent = new Student();
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn1 = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt1 =
                conn1.prepareStatement("SELECT * FROM students WHERE username=? and password=?");
            
            //append values for the parameters
            int i=1;
            
            pstmt1.setString(i++, username);
            pstmt1.setString(i++, password);
            
            //execute SQL statement 
            ResultSet rst = pstmt1.executeQuery();
            
            
            checker=0;
            
            while(rst.next())
            {
                checker++;
                newStudent.setUsername(username);
                newStudent.setPassword(password);
            }
            
        }
        catch(SQLException ex)
        {
        
            System.out.println("s_logIn() has failed to execute");
            
        }
            
            if(checker>0)
                return true;
            else
                return false;
    }
    
    public void viewStudent(Student studentData)
    {
      
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt = conn.prepareStatement("Select * FROM students WHERE username=?");
            
            //append values for the parameters
            int i=1;
            pstmt.setString(i++, studentData.getUsername());
            
            //execute SQL statement
            ResultSet rst = pstmt.executeQuery();
            
            
            while(rst.next())
            {
                studentData.setUsername(rst.getString("username"));
                studentData.setPassword(rst.getString("password"));
                studentData.setS_FirstName(rst.getString("s_firstname"));
                studentData.setS_MI(rst.getString("s_mi"));
                studentData.setS_LastName(rst.getString("s_lastname"));
            }
        }
        catch(SQLException ex)
        {
            System.out.println("viewStudent() has failed to execute");
        }
    }
     
    public boolean st_logIn(String username,String password)
    {
        Staff newStaff = new Staff();
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn1 = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt1 =
                conn1.prepareStatement("SELECT * FROM staff WHERE username=? and password=?");
            
            //append values for the parameters
            int i=1;
            
            pstmt1.setString(i++, username);
            pstmt1.setString(i++, password);
            
            //execute SQL statement 
            ResultSet rst = pstmt1.executeQuery();
            
            
            checker=0;
            
            while(rst.next())
            {
                checker++;
                newStaff.setUsername(username);
                newStaff.setPassword(password);
            }
            
        }
        catch(SQLException ex)
        {
        
            System.out.println("st_logIn() has failed to execute");
            
        }
            
            if(checker>0)
                return true;
            else
                return false;
    }
     
    public void viewStaff(Staff staffData)
    {
      
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt = conn.prepareStatement("Select * FROM staff WHERE username=?");
            
            //append values for the parameters
            int i=1;
            pstmt.setString(i++, staffData.getUsername());
            
            //execute SQL statement
            ResultSet rst = pstmt.executeQuery();
            
            
            while(rst.next())
            {
                staffData.setUsername(rst.getString("username"));
                staffData.setPassword(rst.getString("password"));
                staffData.setStaffType(rst.getString("staffType"));
                staffData.setSt_FirstName(rst.getString("st_firstname"));
                staffData.setSt_MI(rst.getString("st_mi"));
                staffData.setSt_LastName(rst.getString("st_lastname"));
                staffData.setDepartment(rst.getString("department"));
            }
        }
        catch(SQLException ex)
        {
            System.out.println("viewStaff() has failed to execute");
        }
    }
    
    public boolean addGlobal(Announcement newGlobal)
    {
        
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt =
                conn.prepareStatement("INSERT INTO announcements(title,type,body,datePosted,department) VALUES(?,?,?,?,?)");

            
            //append values for the parameters
            
            // for log purposes
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            java.util.Date date = new java.util.Date();
            String currentDate = dateFormat.format(date);
            
            /* for user*/
            int i=1;
            pstmt.setString(i++, newGlobal.getAddTitle());
            pstmt.setString(i++, newGlobal.getType());
            pstmt.setString(i++, newGlobal.getAddBody());
            pstmt.setString(i++, currentDate);
            pstmt.setString(i++, newGlobal.getDepartment());
            
            pstmt.executeUpdate();

            newGlobal.setAddDatePosted(currentDate);
        }catch(SQLException ex)
        {
            
            System.out.println("addGlobal() has failed to execute");
            return false;
        }
        
            
        return true;
            
        
        
    }
    
    public boolean addDepartmental(Announcement newDepartmental)
    {
        
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt =
                conn.prepareStatement("INSERT INTO announcements(title,type,body,datePosted,department) VALUES(?,?,?,?,?)");

            
            //append values for the parameters
            
            // for log purposes
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            java.util.Date date = new java.util.Date();
            String currentDate = dateFormat.format(date);
            
            /* for user*/
            int i=1;
            pstmt.setString(i++, newDepartmental.getAddTitle());
            pstmt.setString(i++, newDepartmental.getType());
            pstmt.setString(i++, newDepartmental.getAddBody());
            pstmt.setString(i++, currentDate);
            pstmt.setString(i++, newDepartmental.getDepartment());
            
            pstmt.executeUpdate();

            newDepartmental.setAddDatePosted(currentDate);
        }catch(SQLException ex)
        {
            
            System.out.println("addDepartmental() has failed to execute");
            return false;
        }
        
            
        return true;
            
        
        
    }
    
   
     
     public void viewGlobal(Announcement g_Announce)
    {
        int ctr=0;
        String[] title = new String[20];
        String[] body = new String[20];
        String[] dateposted = new String[20];
        
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt = conn.prepareStatement("Select * FROM announcements WHERE type=?");
            //append values for the parameters
            int i=1;
            pstmt.setString(i++, g_Announce.getType());
            
            //execute SQL statement
            ResultSet rst = pstmt.executeQuery();
            
            
            while(rst.next())
            {
                title[ctr] = rst.getString("title");
                body[ctr] = rst.getString("body");
                dateposted[ctr] = rst.getString("datePosted");
                ctr++;
            }
            g_Announce.setTitle(title);
            g_Announce.setBody(body);
            g_Announce.setDatePosted(dateposted);
            g_Announce.setCount(ctr);
        }
        catch(SQLException ex)
        {
            System.out.println("viewGlobal() has failed to execute");
        }
    }
    
    public void viewDepartmental(Announcement d_Announce)
    {
        int ctr=0;
        String[] title = new String[25];
        String[] body = new String[25];
        String[] dateposted = new String[25];
        
        
        try{
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();
            
            //Construct SQL Query
            PreparedStatement pstmt = conn.prepareStatement("Select * FROM announcements WHERE type=? and department=?");
            //append values for the parameters
            int i=1;
            pstmt.setString(i++, d_Announce.getType());
            pstmt.setString(i++, d_Announce.getDepartment());
            
            //execute SQL statement
            ResultSet rst = pstmt.executeQuery();
            
            
            while(rst.next())
            {
                title[ctr] = rst.getString("title");
                body[ctr] = rst.getString("body");
                dateposted[ctr] = rst.getString("datePosted");
                ctr++;
            }
            d_Announce.setTitle(title);
            d_Announce.setBody(body);
            d_Announce.setDatePosted(dateposted);
            d_Announce.setCount(ctr);
        }
        catch(SQLException ex)
        {
            System.out.println("viewDepartmental() has failed to execute");
        }
    }
    
    
}
